local modspecialkey = "mymod" --需要修改，每个模组都应该不一样
--[[ continue_down--客户端按住的时候持续发送rpc到服务端 ]]
local keylist = {
    {
        key = KEY_R,
        cooldown = 5, --下面的某个serverfn的返回值为true才会触发cooldown
        not_checkingame = false,
        checkplayer = function(player)
            return player:HasTag("herta")
        end,
        paramsfn = function()
            return 0.5, 1, 0.5
        end,
        clientside_fn = function(player, mpos, mouseover)
        end,
        clientside_fn_down = function(player, mpos, mouseover)
        end,
        clientside_fn_up = function(player, mpos, mouseover)
        end,
        serverside_fn = function(player, mpos, mouseover)
        end,
        serverside_fn_down = function(player, mpos, mouseover)
        end,
        serverside_fn_up = function(player, mpos, mouseover, ...)
            print(...)
        end,
    },
}
local function InGame()
    return ThePlayer and ThePlayer.HUD and not ThePlayer.HUD:HasInputFocus()
end
for i, v in ipairs(keylist) do
    local nowindex = i
    local nowhandlername = "keyhandler" .. nowindex
    TheInput:AddKeyDownHandler(v.key, function()
        local ingame = InGame()
        if (ingame or v.not_checkingame) and (not v.checkplayer or v.checkplayer(ThePlayer)) then
            local keyindex = modspecialkey .. tostring(nowindex)
            local keydownindex = keyindex .. "clientdown"
            local mpos = TheInput:GetWorldPosition()
            local mouseover = TheInput:GetWorldEntityUnderMouse()
            if not ThePlayer[keydownindex] or v.continue_down then
                ThePlayer[keydownindex] = true
                if v.clientside_fn then
                    v.clientside_fn(ThePlayer, TheInput:GetWorldPosition(), mouseover)
                end
                if v.clientside_fn_down then
                    v.clientside_fn_down(ThePlayer, TheInput:GetWorldPosition(), mouseover)
                end
                local paramsfn = v.paramsfn
                local params = {}
                if paramsfn then
                    params = { paramsfn() }
                end
                SendModRPCToServer(MOD_RPC[modspecialkey][nowhandlername], true, mpos.x, mpos.z,
                    mouseover, unpack(params))
            end
        end
    end)
    TheInput:AddKeyUpHandler(v.key, function()
        local ingame = InGame()
        if (ingame or v.not_checkingame) and (not v.checkplayer or v.checkplayer(ThePlayer)) then
            local keyindex = modspecialkey .. tostring(nowindex)
            local keydownindex = keyindex .. "clientdown"
            local mpos = TheInput:GetWorldPosition()
            local mouseover = TheInput:GetWorldEntityUnderMouse()

            ThePlayer[keydownindex] = nil
            if v.clientside_fn then
                v.clientside_fn(ThePlayer, TheInput:GetWorldPosition(), mouseover)
            end
            if v.clientside_fn_up then
                v.clientside_fn_up(ThePlayer, TheInput:GetWorldPosition(), mouseover)
            end
            local paramsfn = v.paramsfn
            local params = {}
            if paramsfn then
                params = { paramsfn() }
            end
            SendModRPCToServer(MOD_RPC[modspecialkey][nowhandlername], nil, mpos.x, mpos.z,
                mouseover, unpack(params))
        end
    end)
    AddModRPCHandler(modspecialkey, nowhandlername, function(player, down, x, z, mouseover, ...)
        local timer = player.components.timer
        if not v.checkplayer or v.checkplayer(player) then
            local keyindex = modspecialkey .. tostring(nowindex)
            local keydownindex = keyindex .. "serverdown"
            local cooldownkey = keyindex .. "cooldown"
            local incooldown = timer:TimerExists(cooldownkey)
            if not incooldown then
                local mpos = Vector3(x, 0, z)
                local success = false
                if down then
                    if not player[keydownindex] or v.continue_down then
                        player[keydownindex] = true
                        if v.serverside_fn and v.serverside_fn(player, mpos, mouseover, ...) then
                            success = true
                        end
                        if v.serverside_fn_down and v.serverside_fn_down(player, mpos, mouseover, ...) then
                            success = true
                        end
                    end
                else
                    player[keydownindex] = nil
                    if v.serverside_fn and v.serverside_fn(player, mpos, mouseover, ...) then
                        success = true
                    end
                    if v.serverside_fn_up and v.serverside_fn_up(player, mpos, mouseover, ...) then
                        success = true
                    end
                end
                if success and v.cooldown then
                    timer:StartTimer(cooldownkey, v.cooldown)
                end
            end
        end
    end)
end
